import sys

import GraphStat.NetworkBuilder as nb
import GraphStat.Visualization as vl


if __name__ == "__main__":

    # 导入命令行参数
    NETWORK_PATH = sys.argv[1]  # newmovies.txt
    TMP_PATH = sys.argv[2]  # tmp.json
    NODE_ATTR = sys.argv[3].split(";")  # "id;name;weight;type;other_info"

    print(f"\n数据路径为 {NETWORK_PATH}")
    print(f"缓存文件为 {TMP_PATH}")
    print(f"节点属性有 {NODE_ATTR}")

    # 读取数据
    with open(NETWORK_PATH, "r", encoding='utf8') as f:
        lines = f.readlines()

    # 处理数据
    k = int(lines[0].split()[1])
    Vertices = [line.split("\t") for line in lines[1:k + 2]]
    Edge = []
    for line in lines[k + 3:]:
        z = line.split()
        Edge.append(z[0:2] + [int(z[2])])

    # 将节点信息和边信息组合成网络
    node_list = [nb.node.init_node(NODE_ATTR, vertex) for vertex in Vertices]
    G = nb.graph.init_graph(node_list, Edge)

    # 缓存功能
    nb.save_graph(G, TMP_PATH)
    G1 = nb.load_graph(TMP_PATH)
    print(f"\n缓存成功：{G==G1}")

    # 获得某些节点的信息
    id = 12456
    print(f"\n以下是id为{id}的信息：\n")
    nb.print_node(G["vertices"][12456])

    # 计算平均度
    avg_degree = nb.stat.cal_average_dgree(G)
    print(f"\n给定图的平均度为：{avg_degree}")
"""
    # 绘制度分布图
    vl.plotgraph.plotdgree_distribution(G)

    # 绘制权重分布图
    vl.plotnodes.plot_nodes_attr(G, "weight", is_numeric=True)

    # 绘制类型分布图
    vl.plotnodes.plot_nodes_attr(G, "type")
"""